package com.pcbly.web.action.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.pcbly.web.Constants;
import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.client.CustomerBean;
import com.pcbly.web.bean.client.CustomerListBean;
import com.pcbly.web.bean.dict.DictCategoryValueBean;
import com.pcbly.web.bean.system.ProvinceCityBean;
import com.pcbly.web.business.client.CustomerService;
import com.pcbly.web.business.dict.DictCategoryCombService;
import com.pcbly.web.business.system.ProvinceCityService;
import com.pcbly.web.components.form.AnalyseCommonDataExt;
import com.pcbly.web.components.hibernate.support.Page;

public class CustomerAction extends BaseAction<CustomerBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1935611645798683804L;

	private CustomerService customerService;

	private ProvinceCityService provinceCityService;

	private CustomerBean customerBean;

	private List<CustomerBean> customerBeans;

	private Page<CustomerListBean> customerBeanPages;

	private DictCategoryCombService dictCategoryCombService;

	private String orderBy = "clientName";

	private boolean asc = true;

	private List<DictCategoryValueBean> customerLevels;

	private List<DictCategoryValueBean> customerSources;

	private List<DictCategoryValueBean> customerKinds;

	private List<DictCategoryValueBean> customerTrades;

	private List<DictCategoryValueBean> customerRegions;

	private List<DictCategoryValueBean> customerDealings;

	private List<DictCategoryValueBean> customerScales;

	private List<ProvinceCityBean> provinceBeans;

	private List<ProvinceCityBean> cityBeans;

	private List<HashMap<String, String>> flags;

	public List<HashMap<String, String>> getFlags() {
		return flags;
	}

	public void setFlags(List<HashMap<String, String>> flags) {
		this.flags = flags;
	}

	public List<ProvinceCityBean> getProvinceBeans() {
		return provinceBeans;
	}

	public List<DictCategoryValueBean> getCustomerRegions() {
		return customerRegions;
	}

	public void setCustomerRegions(List<DictCategoryValueBean> customerRegions) {
		this.customerRegions = customerRegions;
	}

	public List<DictCategoryValueBean> getCustomerDealings() {
		return customerDealings;
	}

	public void setCustomerDealings(List<DictCategoryValueBean> customerDealings) {
		this.customerDealings = customerDealings;
	}

	public List<DictCategoryValueBean> getCustomerScales() {
		return customerScales;
	}

	public void setCustomerScales(List<DictCategoryValueBean> customerScales) {
		this.customerScales = customerScales;
	}

	public void setProvinceBeans(List<ProvinceCityBean> provinceBeans) {
		this.provinceBeans = provinceBeans;
	}

	public List<ProvinceCityBean> getCityBeans() {
		return cityBeans;
	}

	public void setCityBeans(List<ProvinceCityBean> cityBeans) {
		this.cityBeans = cityBeans;
	}

	public List<DictCategoryValueBean> getCustomerLevels() {
		return customerLevels;
	}

	public void setCustomerLevels(List<DictCategoryValueBean> customerLevels) {
		this.customerLevels = customerLevels;
	}

	public List<DictCategoryValueBean> getCustomerSources() {
		return customerSources;
	}

	public void setCustomerSources(List<DictCategoryValueBean> customerSources) {
		this.customerSources = customerSources;
	}

	public List<DictCategoryValueBean> getCustomerKinds() {
		return customerKinds;
	}

	public void setCustomerKinds(List<DictCategoryValueBean> customerKinds) {
		this.customerKinds = customerKinds;
	}

	public List<DictCategoryValueBean> getCustomerTrades() {
		return customerTrades;
	}

	public void setCustomerTrades(List<DictCategoryValueBean> customerTrades) {
		this.customerTrades = customerTrades;
	}

	public void setDictCategoryCombService(
			DictCategoryCombService dictCategoryCombService) {
		this.dictCategoryCombService = dictCategoryCombService;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}

	public String index() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("log index...........");
		}
		long startTime = System.currentTimeMillis();
		long start = getPage();
		start = start * getPageSize();
		if (orderBy == null || "".equals(orderBy)) {
			orderBy = "clientName";
		}

		customerLevels = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_LEVEL);

		DictCategoryValueBean option = new DictCategoryValueBean();
		option.setId("0");
		option.setName("----------------");
		customerLevels.add(0, option);

		customerKinds = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_KIND);

		customerKinds.add(0, option);

		customerSources = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_SOURCE);

		customerSources.add(0, option);

		customerTrades = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_TRADE);
		customerTrades.add(0, option);

		customerBeanPages = new Page<CustomerListBean>();
		customerBeanPages.setStart(start);
		customerBeanPages.setPageSize(getPageSize());
		customerBeanPages = customerService.findCustomerBeanByCond(
				customerBean, customerBeanPages, orderBy, asc);
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("page:::::" + getPage());
			log.debug("pageSize:::::" + getPageSize());
			log.debug("time ms:::::" + startTime);

			System.out.println("page:::::" + getPage());
			System.out.println("pageSize:::::" + getPageSize());
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}

	private void fillFlags() {
		flags = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("id", "1");
		map.put("value", getText("entity.yes"));
		flags.add(map);
		map = new HashMap<String, String>();
		map.put("id", "0");
		map.put("value", getText("entity.no"));
		flags.add(map);

	}

	public String update() throws Exception {
		setAttributes();
		customerBean = customerService.getCustomerBeanById(getId());

		if (!(customerBean.getProvinceId() == null || "0".equals(customerBean
				.getProvinceId()))) {

			cityBeans = provinceCityService
					.findProvinceCityBeansByParentId(customerBean
							.getProvinceId());
		} else {
			ProvinceCityBean pcoption = new ProvinceCityBean();
			pcoption.setId("0");
			pcoption.setName("----------------");
			cityBeans = new ArrayList<ProvinceCityBean>();
			cityBeans.add(pcoption);
		}
		extendCustomer = customerService.findCustomerBeanExtEdit(
				"t_customer_ext", getId());

		return super.update();
	}

	public String view() throws Exception {
		customerBean = customerService.getCustomerBeanById(getId());
		extendCustomer = customerService.findCustomerBeanExtView(
				"t_customer_ext", getId());
		DictCategoryValueBean value;
		if (!(customerBean.getClientLevel() == null || "0".equals(customerBean
				.getClientLevel()))) {// CUSTOMER_LEVEL
			value = dictCategoryCombService
					.findDictCategoryValueBeanById(customerBean
							.getClientLevel());
			// System.out.println(customerBean.getClientLevel());
			// System.out.println(value.getName());
			if (!(value == null))
				customerBean.setClientLevel(value.getName());
		}
		if (!(customerBean.getClientKind() == null || "0".equals(customerBean
				.getClientKind()))) {// CUSTOMER_KIND
			value = dictCategoryCombService
					.findDictCategoryValueBeanById(customerBean.getClientKind());
			// System.out.println(customerBean.getClientKind());
			// System.out.println(value.getName());
			if (!(value == null))
				customerBean.setClientKind(value.getName());
		}
		if (!(customerBean.getClientSource() == null || "0".equals(customerBean
				.getClientSource()))) {// CUSTOMER_SOURCE
			value = dictCategoryCombService
					.findDictCategoryValueBeanById(customerBean
							.getClientSource());
			// System.out.println(customerBean.getClientSource());
			// System.out.println(value.getName());
			if (!(value == null))
				customerBean.setClientSource(value.getName());
		}
		if (!(customerBean.getClientTrade() == null || "0".equals(customerBean
				.getClientTrade()))) {// CUSTOMER_TRADE
			value = dictCategoryCombService
					.findDictCategoryValueBeanById(customerBean
							.getClientTrade());
			// System.out.println(customerBean.getClientTrade());
			// System.out.println(value.getName());
			if (!(value == null)) {
				customerBean.setClientTrade(value.getName());
			}
		}
		if (!(customerBean.getRegionId() == null || "0".equals(customerBean
				.getRegionId()))) {// CUSTOMER_REGION
			value = dictCategoryCombService
					.findDictCategoryValueBeanById(customerBean.getRegionId());
			// System.out.println(customerBean.getClientTrade());
			// System.out.println(value.getName());
			if (!(value == null)) {
				customerBean.setRegionId(value.getName());
			}
		}
		if (!(customerBean.getDealing() == null || "0".equals(customerBean
				.getDealing()))) {// CUSTOMER_DEALING
			value = dictCategoryCombService
					.findDictCategoryValueBeanById(customerBean.getDealing());
			// System.out.println(customerBean.getClientTrade());
			// System.out.println(value.getName());
			if (!(value == null)) {
				customerBean.setDealing(value.getName());
			}
		}
		if (!(customerBean.getClientScale() == null || "0".equals(customerBean
				.getClientScale()))) {// CUSTOMER_SCALE
			value = dictCategoryCombService
					.findDictCategoryValueBeanById(customerBean
							.getClientScale());
			// System.out.println(customerBean.getClientTrade());
			// System.out.println(value.getName());
			if (!(value == null)) {
				customerBean.setClientScale(value.getName());
			}
		}
		ProvinceCityBean provinceCityBean;

		if (!(customerBean.getProvinceId() == null || "0".equals(customerBean
				.getProvinceId()))) {
			provinceCityBean = provinceCityService
					.getProvinceCityBeanById(customerBean.getProvinceId());
			// System.out.println(customerBean.getClientTrade());
			// System.out.println(value.getName());

			if (!(provinceCityBean == null)) {
				customerBean.setProvinceId(provinceCityBean.getName());
			}

		}
		if (!(customerBean.getCityId() == null || "0".equals(customerBean
				.getCityId()))) {
			provinceCityBean = provinceCityService
					.getProvinceCityBeanById(customerBean.getCityId());
			// System.out.println(customerBean.getClientTrade());
			// System.out.println(value.getName());
			if (!(provinceCityBean == null)) {
				customerBean.setCityId(provinceCityBean.getName());
			}
		}
		return super.view();
	}

	public String add() throws Exception {
		extendCustomer = customerService
				.findCustomerBeanExtCreate("t_customer_ext");
		customerBean = new CustomerBean();
		setAttributes();
		ProvinceCityBean pcoption = new ProvinceCityBean();
		pcoption.setId("0");
		pcoption.setName("----------------");
		cityBeans = new ArrayList<ProvinceCityBean>();
		cityBeans.add(pcoption);
		return super.add();
	}

	public String dwrDelete(String id) throws Exception {
		try {
			customerService.deleteCustomerBeanById(id);
			return super.dwrBaseDelete(id);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return ERROR;
		}
	}

	public String save() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug(feedbackKeyAndValue);
			System.out.println(feedbackKeyAndValue);
		}
		AnalyseCommonDataExt ext = new AnalyseCommonDataExt();
		ext.setTableName("t_customer_ext");
		ext.setFeedbackKeyAndValue(feedbackKeyAndValue);

		if (customerBean.getId() == null
				|| "".equals(customerBean.getId().trim())) {
			customerBean.setCreateDate(new Date());
			customerBean.setCreateUserId(getUserId());
			customerBean.setRecycled("0");
			customerBean = customerService.saveCustomerBean(customerBean);
			// List<String> messages=new ArrayList<String>();
			ext.setId(customerBean.getId());
			String sql = ext.execute();
			if (!(sql == null || "".equals(sql.trim()))) {
				customerService.saveCustomerBeanExt(sql);
			}
			setMyMessage(getText("entity.saved"));
			// super.setActionMessages(messages);
		} else {
			customerBean = customerService.updateCustomerBean(customerBean);
			boolean iflag = customerService.getHasCustomerBeanExtData(
					"t_customer_ext", customerBean.getId());
			ext.setHaveData(iflag);
			ext.setId(customerBean.getId());
			String sql = ext.execute();
			if (!(sql == null || "".equals(sql.trim()))) {
				customerService.saveCustomerBeanExt(sql);
			}
			// List<String> messages=new ArrayList<String>();
			setMyMessage(getText("entity.updated"));
			// super.setActionMessages(messages);
		}
		setAttributes();
		extendCustomer = customerService.findCustomerBeanExtEdit(
				"t_customer_ext", customerBean.getId());
		return super.save();
	}

	private void setAttributes() throws Exception {

		fillFlags();
		setMyMessage(null);

		customerLevels = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_LEVEL);
		DictCategoryValueBean option = new DictCategoryValueBean();
		option.setId("0");
		option.setName("----------------");
		customerLevels.add(0, option);

		customerKinds = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_KIND);

		customerKinds.add(0, option);

		customerSources = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_SOURCE);
		customerSources.add(0, option);

		customerTrades = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_TRADE);

		customerTrades.add(0, option);

		customerRegions = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_REGION);

		customerRegions.add(0, option);

		customerDealings = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_DEALING);

		customerDealings.add(0, option);

		customerScales = dictCategoryCombService
				.queryDictCategoryValueBeanById(Constants.CUSTOMER_SCALE);
		customerScales.add(0, option);

		provinceBeans = provinceCityService
				.findProvinceCityBeansByParentId("0");

	}

	public CustomerBean getCustomerBean() {
		return customerBean;
	}

	public void setCustomerBean(CustomerBean customerBean) {
		this.customerBean = customerBean;
	}

	public List<CustomerBean> getCustomerBeans() {
		return customerBeans;
	}

	public void setCustomerBeans(List<CustomerBean> customerBeans) {
		this.customerBeans = customerBeans;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public Page<CustomerListBean> getCustomerBeanPages() {
		return customerBeanPages;
	}

	public void setCustomerBeanPages(Page<CustomerListBean> customerBeanPages) {
		this.customerBeanPages = customerBeanPages;
	}

	public void setProvinceCityService(ProvinceCityService provinceCityService) {
		this.provinceCityService = provinceCityService;
	}

}
